package com.ea.service.impl;

import com.ea.common.enums.OperationResultCode;
import com.ea.common.enums.YesOrNoEnum;
import com.ea.common.exception.ServiceException;
import com.ea.common.util.IdWorker;
import com.ea.domain.dto.StoreEvaluateDTO;
import com.ea.domain.entity.OrderFormDetail;
import com.ea.domain.entity.OrderFormInfo;
import com.ea.domain.entity.StoreEvaluateInfo;
import com.ea.domain.query.EvaluateQueryDTO;
import com.ea.domain.vo.OrderDetailVO;
import com.ea.domain.vo.StoreEvaluateVO;
import com.ea.holder.UserHolderInfo;
import com.ea.mapper.OrderFormDetailMapper;
import com.ea.mapper.OrderFormInfoMapper;
import com.ea.mapper.StoreEvaluateInfoMapper;
import com.ea.service.IStoreEvaluateInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author NAME-L
 * @Description TODO
 * @className IStoreEvaluateInfoServiceImpl
 * @date 2023-01-13 00:09:04
 */
@Service
public class IStoreEvaluateInfoServiceImpl implements IStoreEvaluateInfoService {

    private static final Logger logger = LoggerFactory.getLogger(IStoreEvaluateInfoServiceImpl.class);

    @Autowired
    private StoreEvaluateInfoMapper evaluateInfoMapper;

    @Autowired
    private OrderFormInfoMapper orderMapper;

    @Autowired
    private OrderFormDetailMapper orderDetailMapper;

    @Override
    public PageInfo<StoreEvaluateVO> getEvaluateList(EvaluateQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageIndex(), queryDTO.getPageSize());
        List<StoreEvaluateVO> evaluateList = evaluateInfoMapper.getEvaluateList(queryDTO);
        return new PageInfo<>(evaluateList);
    }

    @Override
    public List<StoreEvaluateVO> listEvaluateByGoodsId(Long goodsId) {
        logger.info("查询货品评价信息 ====> goodsId:{}", goodsId);
        if (goodsId == null) {
            return new ArrayList<>();
        }
        return evaluateInfoMapper.listEvaluateByGoodsId(goodsId);
    }

    @Override
    public List<OrderDetailVO> listCanEvaluateGoods(Long orderId) {
        OrderFormInfo orderInfo = orderMapper.selectOneByOrderId(orderId);
        if (orderInfo == null) {
            throw new ServiceException(OperationResultCode.ORDER_NOT_EXISTS);
        }
        List<OrderDetailVO> orderDetailList = orderDetailMapper.selectCanDeliverDetail(orderId);
        if (CollectionUtils.isEmpty(orderDetailList)) {
            return new ArrayList<>();
        }
        return orderDetailList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void evaluateGoodsBySkuId(StoreEvaluateDTO evaluateDTO) {
        Long orderId = evaluateDTO.getOrderId();
        Long skuId = evaluateDTO.getSkuId();
        Integer level = evaluateDTO.getLevel();
        if (orderId == null || skuId == null || level == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        OrderFormInfo orderInfo = orderMapper.selectOneByOrderId(orderId);
        // 校验订单是否归属于当前用户
        if (!orderInfo.getUserId().equals(UserHolderInfo.getUserId())) {
            throw new ServiceException(OperationResultCode.ORDER_NOT_EXISTS);
        }
        List<OrderFormDetail> orderDetailList = orderDetailMapper.selectDetailByOrderId(orderId);
        if (CollectionUtils.isEmpty(orderDetailList)) {
            logger.info("该订单下明细信息为空");
            return;
        }
        List<Long> skuIdList = orderDetailList.stream().map(OrderFormDetail::getSkuId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(skuIdList) || !skuIdList.contains(skuId)) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);
        }
        StoreEvaluateInfo evaluateInfo = new StoreEvaluateInfo();
        BeanUtils.copyProperties(evaluateDTO, evaluateInfo);
        evaluateInfo.setEvaluateId(IdWorker.nextId());
        evaluateInfo.setUserId(UserHolderInfo.getUserId());

        // 添加评论信息
        evaluateInfoMapper.createOneEvaluate(evaluateInfo);
        // 更新订单明细表是否评价
        orderDetailMapper.updateIsEvaluate(orderId, skuId, YesOrNoEnum.YES.getCode());

    }

    @Override
    public void deleteEvaluateInfo(Long evaluateId) {

    }

}
